//
//  YTXTools.cpp
//  Chart
//
//  Created by CaoJun on 15/2/15.
//
//

#include "YTXChartTools.h"

using namespace ytx;
using namespace std;

string Tools::Convert2FixString(double value, int toFix){
    ostringstream oss;
    oss << std::fixed << std::setprecision(toFix) << value;
    return oss.str();
}

string Tools::numberToFixedPercentString(double valuePercent, int toFixed)
{
    ostringstream oss;
    oss << fixed << setprecision(toFixed) << valuePercent << '%';
    return oss.str();
}

string Tools::random(){
    return ConvertToString(rand());
}

double Tools::convert2Fix(double value, int toFix){
    return atof(Convert2FixString(value, toFix).c_str());
}

int Tools::parseInt(string s){
    int ret = 0;
    sscanf(s.c_str(), "%d", &ret);
    return ret;
}

long Tools::parseLong(string s){
    long ret = 0;
    sscanf(s.c_str(), "%ld", &ret);
    return ret;
}

float Tools::parseFloat(string s){
    float ret = 0;
    sscanf(s.c_str(), "%f", &ret);
    return ret;
}

double Tools::parseDouble(string s){
    double ret = 0;
    sscanf(s.c_str(), "%lf", &ret);
    return ret;
}

vector<double> Tools::slice(vector<double> data, int start, int count)
{
    //    if (data.size() == 0) {
    //        return ;
    //    }
    if (data.size() - start < count) {
        count = (int)data.size() - start;
    }
    vector<double> result(count);
    int len = start + count;
    int j = 0;
    for (; start < len; start++) {
        result[j++] = data[start];
    }
    return result;
}

void Tools::swapFloat(float *f1, float *f2)
{
    float temp;
    temp = *f1;
    *f1 = *f2;
    *f2 = temp;
}
/* Converts an integer value to its hex character*/
char Tools::to_hex(char code) {
    static char hex[] = "0123456789abcdef";
    return hex[code & 0xf];
}


string Tools::url_encode(const string & str)
{
    ostringstream oss;
    for (string::const_iterator i = str.begin(); i != str.end(); ++i) {
        if (isalnum(*i) || *i == '-' || *i == '_' || *i == '.' || *i == '~') {
            oss << *i;
        } else if (*i == ' ') {
            oss << "%20";
        } else {
            oss << '%' << to_hex(*i >> 4) << to_hex(*i & 0xf);
        }
    }
    return oss.str();
}

/* Returns a url-decoded version of str */
/* IMPORTANT: be sure to free() the returned string after use */
//char * Tools::url_decode(char *str) {
//    char *pstr = str, *buf = malloc(strlen(str) + 1), *pbuf = buf;
//    while (*pstr) {
//        if (*pstr == '%') {
//            if (pstr[1] && pstr[2]) {
//                *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]);
//                pstr += 2;
//            }
//        } else if (*pstr == '+') {
//            *pbuf++ = ' ';
//        } else {
//            *pbuf++ = *pstr;
//        }
//        pstr++;
//    }
//    *pbuf = '\0';
//    return buf;
//}

bool Tools::compareForMax(double a, double b)
{
    if ( isnan(a) ) {
        return true;
    }
    if ( isnan(b) ) {
        return false;
    }
    return a < b;
}


bool Tools::compareForMin(double a, double b)
{
    if ( isnan(b) ) {
        return true;
    }
    if ( isnan(a) ) {
        return false;
    }
    return a < b;
}

std::vector<std::string> &Tools::split(const std::string &s, char delim, std::vector<std::string> &elems) {
    std::stringstream ss(s);
    std::string item;
    while (std::getline(ss, item, delim)) {
        elems.push_back(item);
    }
    return elems;
}


std::vector<std::string> Tools::split(const std::string &s, char delim) {
    std::vector<std::string> elems;
    split(s, delim, elems);
    return elems;
}

bool Tools::replace(std::string& str, const std::string& from, const std::string& to) {
    size_t start_pos = str.find(from);
    if(start_pos == std::string::npos)
        return false;
    str.replace(start_pos, from.length(), to);
    return true;
}


